/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *      pub_log.cpp
 * 位 置:
 *      src/biz/pug_log.cpp
 * 描 述:
 *      公用日志类
 * 备 注:
 *      无
 * 修 改:
 *      2008-09-21  Simon   - 创建
 *
 **************************************************************/

#include <string>
#include <iostream>
#include "occi.h"
#include "pub/db.h"
#include "pub/log.h"
#include "pub/str.h"
#include "pub/util.h"
#include "biz/pub_serial.h"
#include "biz/pub_log.h"

using namespace std;
using namespace oracle::occi;

//----------------------------------------------
//  构造函数
//----------------------------------------------
PubLog::PubLog(Connection* conn)
{
	this->conn = conn;
	returnCode = "0000";
	returnMsg = "";
}

//----------------------------------------------
//  析构函数
//----------------------------------------------
PubLog::~PubLog()
{
	this->conn = 0;
	returnCode = "0000";
	returnMsg = "";
}

//----------------------------------------------
//	写操作日志
//----------------------------------------------
void PubLog::writeLog()
{
	PubSerial serial(conn);
	serial.tradeDate = opDate;
	serial.genLogSerial();
	opLogSerialNo = serial.logSerialNo;

	Statement* stmt = conn->createStatement();
	try
	{
		Log::debug("写入操作日志...");
		string sql = " \
		insert into LogOp \
		( \
			opLogSerialNo, \
			serviceCode, \
			orderSerialNo, \
			acceptMethod, \
			channelCode, \
			regionCode, \
			branchCode, \
			opID, \
			opDate, \
			opTime, \
			returnCode, \
			returnMsg, \
			ipAddr, \
			macAddr, \
			summary \
		) \
		values \
		( \
			:opLogSerialNo, \
			:serviceCode, \
			:orderSerialNo, \
			:acceptMethod, \
			:channelCode, \
			:regionCode, \
			:branchCode, \
			:opID, \
			:opDate, \
			:opTime, \
			:returnCode, \
			:returnMsg, \
			:ipAddr, \
			:macAddr, \
			'' \
		)";

		stmt->setSQL(sql);
		stmt->setString(1, opLogSerialNo);
		stmt->setString(2, serviceCode);
		stmt->setString(3, orderSerialNo);
		stmt->setString(4, acceptMethod);
		stmt->setString(5, channelCode);
		stmt->setString(6, regionCode);
		stmt->setString(7, branchCode);
		stmt->setString(8, opID);
		stmt->setString(9, opDate);
		stmt->setString(10, opTime);
		stmt->setString(11, returnCode);
		stmt->setString(12, returnMsg);
		stmt->setString(13, ipAddr);
		stmt->setString(14, macAddr);
		stmt->executeUpdate();
		conn->terminateStatement(stmt);
		conn->commit();
		Log::debug("写入操作日志成功.");
	}
	catch (exception& e)
	{
		Log::error("写操作日志出错. %s", e.what());
		conn->terminateStatement(stmt);
		throw;
	}
}
